Page({
  data: {
    images: [], // 图片列表
    maxImages: 9,// 最大图片数
    content: ''
  },

  inputContent(e) {
    this.data.content = e.detail.value
  },
  // 添加图片
  async addImage() {
    const res = await getApp().$upload('post')
    this.data.images.push(res.data)
    this.setData({
      images: this.data.images
    })
  },

  // 发布动态
  async publishPost() {
    if (!this.data.content && this.data.images.length == 0) {
      wx.showToast({
        title: '请输入内容',
        icon: "none"
      })
      return
    }
    const { images } = this.data;
    const res = await getApp().$request('/api/publish', 'POST', {
      userId: wx.getStorageSync('user').id,
      content: this.data.content,
      imageUrl: this.data.images.join(','),
      like: 0,
      createdAt: getApp().getNowTime()
    })

    // 此处添加表单验证和上传逻辑
    wx.showToast({
      title: '动态发布成功！',
      icon: 'success'
    });
    this.setData({
      content: '',
      images: []
    })
    wx.navigateTo({
      url: '/pages/postDetail/postDetail?postId=' + res.data,
    })
  }
});
